package com.ny.zmb.openapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.entity.PmUserToken;
import com.ny.zmb.openapi.entity.PmUserWalletLog;
import com.ny.zmb.openapi.service.PmUserWalletLogService;
import com.ny.zmb.openapi.ysutil.base.EYinShengBase;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("ys/wallet")
@CrossOrigin
public class UserWalletController extends EYinShengBase {

    @Resource
    private PmUserWalletLogService pmUserWalletLogService;
    /**
     * 查询用户余额明细
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("walletLog")
    public Result walletLog(@RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo,
                            @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize,
                            @RequestParam(name = "moneyType",defaultValue = "") String moneyType){
        PmUserToken userToken = getUserToken();
        Integer userId = userToken.getUserId();
        LambdaQueryWrapper<PmUserWalletLog> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PmUserWalletLog::getUserId,userId);
        wrapper.eq(PmUserWalletLog::getMoneyType,moneyType);
        wrapper.orderByDesc(PmUserWalletLog::getCreatetime);
        Page<PmUserWalletLog> page = new Page<>(pageNo,pageSize);
        return Result.ok(this.pmUserWalletLogService.page(page,wrapper));
    }

    /**
     * 添加记录
     * @return
     */
    @GetMapping("addWalletLog")
    public Result addWalletLog(PmUserWalletLog log){
        PmUserToken userToken = getUserToken();
        log.setUserId(userToken.getUserId());
        if (null == log){
            return Result.fail("参数有误");
        }
        if (null == log.getMoney()){
            return Result.fail("金额不能为空");
        }
        if (null ==  log.getTag() || null == log.getTargetId()){
            return Result.fail("参数有误");
        }
        Boolean status = this.pmUserWalletLogService.addWalletLog(log);
        if (status){
            return Result.ok();
        }
        return Result.fail("操作失败");
    }
}
